﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.DirectX.DirectInput;

namespace SnakeX
{
	/// <summary>
	/// This class represents a keyboard device.
	/// </summary>
	public class SnakeXKeyboard
	{
		/// <summary>
		/// Constructor.
		/// Initializing keyboard device.
		/// </summary>
		/// <param name="form">Form to set keyboard to</param>
		public SnakeXKeyboard(Control form)
		{
			// Initializing keyboard device object.
			keyboard = new Device(SystemGuid.Keyboard);

			// Setting keyboard cooperative level.
			keyboard.SetCooperativeLevel(form, 
				CooperativeLevelFlags.Background |
				CooperativeLevelFlags.NonExclusive);

			// Getting keyboard access.
			keyboard.Acquire();
		}


		/// <returns>Keyboard pressed key.</returns>
		public Key GetPressedKey()
		{
			Key[] pressedKeys = keyboard.GetPressedKeys();

			if (pressedKeys.Length > 0)
			{
				// Returning the keyboard latest pressed key.
				return pressedKeys[pressedKeys.Length - 1];
			}
			else
			{
				// No pressed keys.
				return Key.Space;
			}
		}


		/// <summary>
		/// The keyboard device object.
		/// </summary>
		private Device keyboard;
	}
}
